package com.wens.ui.drawable

import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import androidx.annotation.IntDef
import androidx.core.content.ContextCompat
import com.google.android.material.color.MaterialColors
import com.wens.ui.R


class ProgressWithDoneDrawable(context: Context, var width: Int = -1, var height: Int = -1) :
    Drawable() {

    companion object {
        const val STATE_ORIGIN = 0
        const val STATE_ROTATE = STATE_ORIGIN + 1
        const val STATE_UP = STATE_ROTATE + 1
        const val STATE_DOWN = STATE_UP + 1
        const val STATE_FINISH = STATE_DOWN + 1
    }

    @IntDef(value = [STATE_ORIGIN, STATE_ROTATE, STATE_UP, STATE_DOWN, STATE_FINISH])
    @Retention(value = AnnotationRetention.SOURCE)
    annotation class AnimateState

    @AnimateState
    var animateState = STATE_ORIGIN

    var strokeWidth: Float = 20f

    private val paint = Paint()
    private var progressDrawable: Drawable? = null

    init {
        progressDrawable = ContextCompat.getDrawable(context, R.drawable.progress_medium_holo)
        paint.strokeCap = Paint.Cap.ROUND
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = strokeWidth
        paint.isAntiAlias = true
        paint.color = MaterialColors.getColor(context, R.attr.colorAccent, Color.WHITE)
        progressDrawable?.colorFilter = PorterDuffColorFilter(paint.color, PorterDuff.Mode.SRC_ATOP)
    }

    override fun draw(canvas: Canvas) {
        progressDrawable?.bounds = bounds
        progressDrawable?.draw(canvas)
        when (animateState) {
            STATE_ORIGIN -> {
            }

            STATE_ROTATE -> {

            }
            STATE_UP -> {

            }
            STATE_DOWN -> {

            }
            STATE_FINISH -> {

            }
        }
    }

    override fun setAlpha(alpha: Int) {
        progressDrawable?.alpha = alpha
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSPARENT
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        progressDrawable?.colorFilter = colorFilter
    }

}